# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>

pkgbase=keystone
pkgname=('keystone' 'python-keystone')
pkgver=0.9.2
pkgrel=5
pkgdesc='Lightweight multi-platform, multi-architecture assembler framework'
url='https://www.keystone-engine.org/'
arch=('loong64' 'x86_64')
license=('GPL2')
makedepends=('cmake' 'ninja' 'gcc-libs' 'python' 'python-setuptools')
options=('staticlibs' '!emptydirs')
source=(https://github.com/keystone-engine/keystone/archive/${pkgver}/${pkgbase}-${pkgver}.tar.gz)
sha512sums=('5d4dd30410410bc7848e75a2e31e8ecef6241ad25e21963aa7a07bbea21d95b75e235c80573129014102684c9647710ace3c0776a8401844d7e4b45cab7bd04b')
b2sums=('5654ae6a66ccf6832fbdbb8c1083a2318e413bb833a0fadc4e1374f0de601e3dad9f354b1dec92d9a944621923458e432510299c1b55579ac9bb6b6e90a818de')

prepare() {
  cd ${pkgbase}-${pkgver}
  sed 's|set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS|# set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS|' -i llvm/CMakeLists.txt
}

build() {
  cd ${pkgbase}-${pkgver}
  cmake \
    -B build-shared \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DBUILD_SHARED_LIBS=ON \
    -DLLVM_TARGETS_TO_BUILD=all \
    -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_SHARED_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_INSTALL_RPATH="" \
    -GNinja .
  ninja -C build-shared
  cmake \
    -B build-static \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects" \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -ffat-lto-objects" \
    -DBUILD_SHARED_LIBS=OFF \
    -DLLVM_TARGETS_TO_BUILD=all \
    -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_SHARED_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_INSTALL_RPATH="" \
    -GNinja .
  ninja -C build-static
  (cd bindings/python
    python setup.py build
  )
}

package_keystone() {
  depends=('gcc-libs')
  provides=('libkeystone.so')
  cd ${pkgbase}-${pkgver}
  DESTDIR="${pkgdir}" ninja -C build-shared install
  DESTDIR="${pkgdir}" ninja -C build-static install
  install -Dm 644 samples/* -t "${pkgdir}/usr/share/doc/${pkgname}/samples"
}

package_python-keystone() {
  depends=('python' 'keystone')
  license=('BSD')
  cd ${pkgbase}-${pkgver}/bindings/python
  python setup.py install --root="${pkgdir}" -O1 --skip-build
  install -Dm 755 sample.py -t "${pkgdir}/usr/share/doc/${pkgname}"
  install -Dm 644 LICENSE.TXT -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

# vim: ts=2 sw=2 et:
